/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.base.page;

/**
 * ClassName:OperatorEnum <br/>
 * Date: 2015年5月19日 下午1:27:23 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */

public class OperatorEnum {

	public enum Operator {
		GT("gt",">"), //>
	    GE("ge",">="), //>=
	    LT("lt","<"),//<
	    LE("le","<="),//<=
	    GTLT("gtlt","大于并且小于"),//  a>b&&a<c
	    GELE("gele","大于等于并且小于等于"),//  a>=b&&a<=c
	    EQ("eq","="),// =
	    NE("ne","<>"),//!=
	    IN("in","in"),//in
	    NI("ni","not in"), //t in 
	    BT("bt","包含"), // bet
	    NOTNULL("notnull","is not null"), // is not null
	    NULL("null","is null"), //is null
	    LIKE("like","like"), //ke %a%
	    LLIKE("llike","like"),//ke %a
	    GLIKE("glike","like");//ke a%

		private String key;
		private String value;

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		private Operator(String key, String value) {
			this.key = key;
			this.value = value;
		}
		
		public static Operator getOperatorEnum(String _key) throws OperatorNotFoundException{
			for (Operator color : Operator.values()) {
				if (color.key == _key||color.key.equals( _key))
					return color;
			}
			throw new OperatorNotFoundException("未发现匹配规则");
		}
		
		public static String getOperator(String _key) throws OperatorNotFoundException{
			for (Operator color : Operator.values()) {
				if (color.key == _key||color.key.equals( _key))
					return color.value;
			}
			throw new OperatorNotFoundException("未发现匹配规则");
		}
		
		public String mate(String column,Object obj){
			String sql = "";
			switch (this) {
				case NULL:
				case NOTNULL:
					sql+=(column+" "+this.getValue());
					break;
				case IN:
				case NI:
					sql=(column+ " " + this.getValue()+"(");
					Object[] os = (Object[]) obj;
					for(int i=0;i<os.length;i++){
						sql+="?,";
					}
					sql = sql.substring(0,sql.length()-1);
					sql+=")";
					break;
				case BT:
					sql=(column+ " between ? and "+" ?");
					break;
				case GTLT:
					sql=(column+ " > ? and "+column+" < ?");
					break;
				case GELE:
					sql=(column+ " >= ? and "+column+" <= ?");
					break;
				case LIKE:
					String[] objs = (String[])obj;
					objs[0]=("%"+objs[0]+"%");
					sql=(column+" "+this.getValue()+" ?");
					break;
				case LLIKE:
					String[] objs1 = (String[])obj;
					objs1[0]=("%"+objs1[0]);
					sql=(column+" "+this.getValue()+" ?");
					break;
				case GLIKE:
					String[] objs2 = (String[])obj;
					objs2[0]=(objs2[0]+"%");
					sql=(column+" "+this.getValue()+" ?");
					break;
				default:
					sql=(column+" "+this.getValue()+" ?");
					break;
			}
			return sql;
		}
	}
}